<?php
namespace Home\Controller;
use Think\Controller;
class CollectController extends ZMController {
	public function hcustcollect($hid=NULL,$pdate=NULL){//收集
		if(empty($hid)||empty($pdate)){//传参
			$hid=I('hid');
			$pdate=I('date');
		}
		$Model=M();
		if(!empty($hid)&&!empty($pdate)){
		    header("Content-type: text/html; charset=utf-8");
		    $res=$Model->db(1,'DB_CONFIG1')->query("select a.cid,a.ctime,a.areaid,a.num_0,a.num_1,a.num_4,a.num_5,nval1_2,nval1_5,c.name as cname,d.name as sname,e.name as hospname from ddwk_collect as a left join ddwk_employee as c on a.empid=c.id left join ddwk_employee as d on a.sid=d.id left join ddwk_hospital as e on a.hid=e.hid where a.hid in ($hid) and a.pdate='$pdate'");
		    foreach($res as &$row){
				$cid=$row['cid'];
				$desc='';
                $areaid = $row['areaid'];
				if(!empty($areaid)){
					$r=$Model->query("select areaname from ddwk_area where id in ($areaid)");				
					foreach($r as $row1){
						$desc.=','.$row1['areaname'];
					}
					$desc=ltrim($desc,',');//部门
				}
				$row['custname']=$desc;
				$row['num_3']=$this->collectBag($cid,3);//化学性
				$row['num_2']=$this->collectBag($cid,6);//输液瓶
				$row['num_7']=$this->collectBag($cid,7);//输液袋
				$row['total']=$row['num_0']+$row['num_1']+$row['num_4']+$row['num_5'];
				$row['weight_0']=(double)$this->totalCollectweight($cid,0);
				$row['weight_1']=(double)$this->totalCollectweight($cid,1);
				$row['weight_3']=(double)$this->totalCollectweight($cid,3);
				$row['weight_4']=(double)$this->totalCollectweight($cid,4);
				$row['weight_5']=(double)$this->totalCollectweight($cid,5);
				$row['weight_2']=(double)$this->totalCollectweight($cid,6);
				$row['weight_7']=(double)$this->totalCollectweight($cid,7);	
                $row['totalweight']=$row['weight_0']+$row['weight_1']+$row['weight_2']+$row['weight_3']+$row['weight_4']+$row['weight_5']+$row['weight_7'];				
		    }
			if(ACTION_NAME=='hcustcollect'){
				$this->ajax($res);
			}
			else{
				return $res;
			}
		}
		else{
			$this->errorreturn("error");
		}
	}
	//箱子内袋子信息
	public function collectSmall(){
		$cid=I('cid');
		$Model=M()->db(1,'DB_CONFIG1');
		$Item=$Model->query("select ciid,sno,type,fval1 as weight from ddwk_collect_item where cid=$cid");
		$ciid='';
		foreach($Item as &$v){
			$ciid.=','.$v['ciid'];
			if($v['sno']=='999999999999'){//不显示虚拟箱子
				$v['sno']='';
			}
		}
		$ciid=ltrim($ciid,',');
		if(empty($ciid)){
			$ciid=0;
		}
		$small=$Model->query("select a.iid,a.code as sno,a.areaid,a.num,a.weight,a.time,b.type from ddwk_collect_small as a left join ddwk_collect_item as b on a.iid=b.ciid where a.iid in ($ciid) and a.cid=$cid");//属于这次收集的箱子内这次收集的袋子
		if(empty($small)){
			$small=$Model->query("select a.iid,a.code as sno,a.areaid,a.num,a.weight,a.time,b.type from ddwk_collect_small as a left join ddwk_collect_item as b on a.iid=b.ciid where a.iid in ($ciid)");//以前版本
		}
		foreach($small as &$row){
			$row['ciid']=0;
			if(strlen($row['sno'])==9){
				$code=substr($row['sno'],0,5);
				$areaid=(int)$code;
			}
			else{
				$areaid=$row['areaid'];
			}
			$area=$Model->query("select areaname from ddwk_area where id=$areaid");
			$row['areaname']=$area['0']['areaname'];
		}
		$arr=array_merge($Item,$small);
		$ssmall=$Model->query("select a.iid,a.code as sno,a.areaid,a.num,a.weight,a.time,b.type from ddwk_collect_small as a left join ddwk_collect_item as b on a.iid=b.ciid where a.iid not in ($ciid) and a.cid=$cid");//不属于这次收集的箱子内这次收集的袋子
		$IItem=array();
		$ciid=0;
		foreach($ssmall as &$row){
			$row['ciid']=0;
			if(strlen($row['sno'])==9){
				$code=substr($row['sno'],0,5);
				$areaid=(int)$code;
			}
			else{
				$areaid=$row['areaid'];
			}
			$area=$Model->query("select areaname from ddwk_area where id=$areaid");
			$row['areaname']=$area['0']['areaname'];
			if($row['iid']!=$ciid){//未录入过
			    $IItem=$Model->query("select ciid,sno,type,fval1 as weight from ddwk_collect_item where ciid=".$row['iid']."");//这些袋子所属的箱子
				$arr=array_merge($arr,$IItem);//合并
				$ciid=$row['iid'];//重新赋值
			}
			
		}
		$arr=array_merge($arr,$ssmall);
		$this->ajax($arr);
	}
	private function collectBag($cid,$type=0){//小点袋子数量统计
		$Model=M('CollectItem');
		$res=$Model->db(1,'DB_CONFIG1')->query("select sum(nval2) as nval2 from ddwk_collect_item where type=$type and cid in ($cid)");
		if(empty($res['0']['nval2'])){
			return 0;
		}
		else{
			return $res['0']['nval2'];
		}
	}
	//入库
	public function UnloadSum($hid=NULL,$pdate=NULL){
		if(empty($hid)||empty($pdate)){//传参
			$hid=I('hid');
			$pdate=I('date');
		}
		$Model=M();
		if(!empty($hid)&&!empty($pdate)){
		    header("Content-type: text/html; charset=utf-8");
		    $res=$Model->db(1,'DB_CONFIG1')->query("select a.uid,a.ctime,a.num_0,a.num_1,a.num_4,a.num_5,nval1_2,nval1_5,c.name as cname,d.name as sname,e.name as hospname from ddwk_unload as a left join ddwk_employee as c on a.empid=c.id left join ddwk_employee as d on a.sid=d.id left join ddwk_hospital as e on a.hid=e.hid where a.hid in ($hid) and a.pdate='$pdate'");
		    foreach($res as &$row){
				$cid=$row['uid'];
				$row['num_3']=$this->UnloadBag($cid,3);//化学性
				$row['num_2']=$this->UnloadBag($cid,6);//输液瓶
				$row['num_7']=$this->UnloadBag($cid,7);//输液袋
				$row['total']=$row['num_0']+$row['num_1']+$row['num_4']+$row['num_5'];
				$row['weight_0']=(double)$this->totalUnloadweight($cid,0);
				$row['weight_1']=(double)$this->totalUnloadweight($cid,1);
				$row['weight_3']=(double)$this->totalUnloadweight($cid,3);
				$row['weight_4']=(double)$this->totalUnloadweight($cid,4);
				$row['weight_5']=(double)$this->totalUnloadweight($cid,5);
				$row['weight_2']=(double)$this->totalUnloadweight($cid,6);
				$row['weight_7']=(double)$this->totalUnloadweight($cid,7);	
                $row['totalweight']=$row['weight_0']+$row['weight_1']+$row['weight_2']+$row['weight_3']+$row['weight_4']+$row['weight_5']+$row['weight_7'];				
		    }
			if(ACTION_NAME=='UnloadSum'){
				$this->ajax($res);
			}
			else{
				return $res;
			}
		}
		else{
			$this->errorreturn("error");
		}
	}
	private function UnloadBag($uid,$type=0){//小点袋子数量统计
		$Model=M('UnloadItem')->db(1,'DB_CONFIG1');
		$res=$Model->query("select sum(nval2) as nval2 from ddwk_unload_item where type=$type and uid in ($uid)");
		if(empty($res['0']['nval2'])){
			return 0;
		}
		else{
			return $res['0']['nval2'];
		}
	}
	//箱子内袋子信息
	public function unloadSmall(){
		$uid=I('uid');
		$Model=M()->db(1,'DB_CONFIG1');
		$Item=$Model->query("select uiid as ciid,sno,type,fval1 as weight from ddwk_unload_item where uid=$uid");
		$uiid='';
		foreach($Item as &$v){
			$uiid.=','.$v['ciid'];
			if($v['sno']=='999999999999'){
				$v['sno']='';
			}
		}
		$uiid=ltrim($uiid,',');
		$small=$Model->query("select a.iid,a.code as sno,a.weight,a.time,a.num,b.type from ddwk_unload_small as a left join ddwk_unload_item as b on a.iid=b.uiid where a.iid in ($uiid)");
		foreach($small as &$row){
			$row['ciid']=0;
			//$areaid=$row['areaid'];
			//$area=$Model->query("select areaname from ddwk_area where id=$areaid");
			//$row['areaname']=$area['0']['areaname'];
		}
		$arr=array_merge($Item,$small);
		$this->ajax($arr);
	}
	public function compareyy(){//收集入库转运对比
		$startdate=I('startdate');
		$enddate=I('enddate');
		$hid=I('custid');
		if(empty($hid)||empty($startdate)||empty($enddate)){
			exit(0);
		}
		$pdate=$startdate;
		$arr=array();
		while($pdate<=$enddate){
			$collectnum=$this->collectnum($pdate,$hid);//收集
			$unloadnum=$this->unloadnum($pdate,$hid);//入库
			$transfer=$this->transfer($pdate,$hid);//转运
			$res['pdate']=$pdate;
			$res['collectnum']=$collectnum['num'];
			$res['unloadnum']=$unloadnum['num'];
			$res['ctotal']=$transfer['num'];
			$res['collectweight']=$collectnum['weight'];
			$res['unloadweight']=$unloadnum['weight'];
			$res['weight']=$transfer['weight'];
			$arr[]=$res;
			$pdate=date('Y-m-d',strtotime("+1 day",strtotime($pdate)));	
		}
		$this->ajax($arr);
	}
	private function collectnum($pdate,$hid){//收集
		$Model=M('Collect');
		if(empty($hid)||empty($pdate)){
			exit(0);
		}
		$res=$Model->db(1,'DB_CONFIG1')->query("select cid,num_0,num_1,num_3,num_4,num_5 from ddwk_collect where pdate='$pdate' and hid=$hid");
		    $num=0;
		    $cid='';
		    $arr=array();
		$numt=$this->delrepeat($pdate,$hid);//计算收集重复数字
		foreach($res as $row){
			$num+=$row['num_0']+$row['num_1']+$row['num_3']+$row['num_4']+$row['num_5'];
			$cid.=','.$row['cid'];
		}
		$cid=ltrim($cid,',');
		$num=$num-$numt;//去除重复
		$weight=$this->totalCollectweight($cid);
		$arr['num']=$num;
		$arr['weight']=$weight;
		return $arr;
	}
	private function unloadnum($pdate,$hid){//入库
		$Model=M('Unload');
		if(empty($hid)||empty($pdate)){
			exit(0);
		}
		$res=$Model->db(1,'DB_CONFIG1')->query("select sum(num_0+num_1+num_2+num_3) as num,sum(fval1) as fval1 from ddwk_unload where pdate='$pdate' and hid=$hid");
		$arr=array();
		$numt=$this->delunlorepeat($pdate,$hid);//计算卸箱重复数字
		$num=$res['0']['num']-$numt;//去除重复
		$weight=$res['0']['fval1'];
		$arr['num']=$num;
		$arr['weight']=$weight;
		return $arr;
	}
	private function transfer($pdate,$hid){//转运
		$Model=M('Collect')->db(2,'DB_CONFIG2');
		if(empty($hid)||empty($pdate)){
			exit(0);
		}
		$res=$Model->query("select sum(garbage) as garbage,sum(lwq) as lwq,sum(guil) as guil,sum(qt) as qt,sum(hx) as hx,kg from ddwk_collect where pdate='$pdate' and custid=$hid group by kg");
		$arr=array();
		$num=0;
		$weight=0;
		foreach($res as $row){
			$num+=$row['garbage']+$row['lwq']+$row['guil']+$row['qt']+$row['hx'];
			$kg=$row['kg'];
			$kg=explode(',',$kg);
			$weight+=$kg['0']*$row['garbage']/1000;//感染性重量
			$weight+=$kg['1']*$row['lwq']/1000;//损伤性重量
			$weight+=$kg['2']*$row['guil']/1000;//病理性重量
			$weight+=$kg['3']*$row['qt']/1000;//药物性重量
			$weight+=$kg['4']*$row['hx']/1000;//化学性重量
		}
		$arr['num']=$num;
		$arr['weight']=$weight;
		return $arr;
	}
	private function delrepeat($pdate,$hid){//计算收集重复数字
		$Model=M();
		$rescollect = $Model->db(1,'DB_CONFIG1')->query("select cid from ddwk_collect where hid=$hid and pdate='$pdate'");
		$ccid='';
		foreach($rescollect as $row){
			$ccid.=$row['cid'].',';
		}
		$ccid=rtrim($ccid,',');
		if(!empty($ccid)){
            $rescollectitem = $Model->query("select count(*) as count from ddwk_collect_item where sno <> '000000000000' and sno<>'999999999999' and sno<>'100000000000' and cid in ($ccid) group by sno having count(sno) > 1");
		}
		$totalrecount=0;
		if (!empty($rescollectitem)) {
            foreach ($rescollectitem as $row) {
				$recount = $row['count'] - 1;
				$totalrecount = $totalrecount + $recount;
			}
		}
		return $totalrecount;
	}
	private function delunlorepeat($pdate,$hid){//计算卸箱重复数字
		$Model=M();
		$res=$Model->db(1,'DB_CONFIG1')->query("select b.sno from ddwk_unload as a left join ddwk_unload_item as b on a.uid=b.uid where a.pdate='$pdate' and a.hid=$hid and b.sno<>'000000000000' and b.sno<>'100000000000'");
		$count1=count($res);
		$b=array();
        foreach($res as $row){
			array_push($b,$row['sno']);
		}
		$b=array_unique($b);
		$count2=count($b);
		$num=$count1-$count2;
		return $num;
	}
	public function totalCollectweight($cid,$type=NULL){//计算收集重量
		try{
			if(empty($cid)){
				E('无收集记录');
			}
			else{
				$Model=M()->db(1,'DB_CONFIG1');
				if(isset($type)){
					$where=" and type=$type";
				}
				$res=$Model->query("select fval1,ciid from ddwk_collect_item where cid in ($cid) $where");
				$fval=0;
				$ciid='';
				foreach($res as $row){
					$fval+=$row['fval1'];
					$ciid.=','.$row['ciid'];
				}
				$ciid=ltrim($ciid,',');//所有箱子id
				if(empty($ciid)){
					$ciid=0;
				}
				$small1=$Model->query("select sum(weight) as weight from ddwk_collect_small where iid in ($ciid) and cid not in ($cid) and cid<>0");//不属于这次收集这些箱子的袋子
				$small2=$Model->query("select sum(b.weight) as weight from ddwk_collect_item as a left join ddwk_collect_small as b on a.ciid=b.iid where b.iid not in ($ciid) and b.cid in ($cid) and b.cid<>0 $where");//属于这次收集但是不属于这次收集的箱子且类型一致
				$fval=$fval-$small1['0']['weight']+$small2['0']['weight'];
				if(!empty($fval)){
				    return $fval;
				}
				else{
					E('数据出错');
				}
			}
		}
		catch(\Exception $e){
			$message=$e->getMessage();
			return 0;
		}
	}
	public function totalCweight($cid,$type=NULL){//计算收集重量
		try{
			if(empty($cid)){
				E('无收集记录');
			}
			else{
				$Model=M()->db(1,'DB_CONFIG1');
				if(isset($type)){
					$where=" and type in ($type)";
				}
				$res=$Model->query("select sum(fval1) as weight from ddwk_collect_item where cid in ($cid) $where");
				if(!empty($res['0']['weight'])){
				    return $res['0']['weight'];
				}
				else{
					E('数据出错');
				}
			}
		}
		catch(\Exception $e){
			$message=$e->getMessage();
			return 0;
		}
	}
	private function totalUnloadweight($uid,$type=NULL){//计算卸箱重量
		try{
			if(empty($uid)){
				E('无卸箱记录');
			}
			else{
				$Model=M()->db(1,'DB_CONFIG1');
				if(isset($type)){
					$where=" and type=$type";
				}
				$res=$Model->query("select sum(fval1) as weight from ddwk_unload_item where uid in ($uid) $where");
				if(!empty($res['0']['weight'])){
				    return $res['0']['weight'];
				}
				else{
					E('数据出错');
				}
			}
		}
		catch(\Exception $e){
			$message=$e->getMessage();
			return 0;
		}
	}
}